using GameKit.Core;
using GameKit.Core.Context;
using GameKitUnity.Core;
using GameKitUnity.Network;
using GameKitUnity.UI;
using UnityEngine;
using Logger = GameKitUnity.Logger;

namespace OneGame
{
    public class GlobalEntry : MonoBehaviour
    {
        private static bool _sInitialized;

        private void Awake()
        {
            DontDestroyOnLoad(gameObject);
        }

        private void Start()
        {
            if (!_sInitialized)
            {
                Initialize();
                _sInitialized = true;
            }
        }

        private static void Initialize()
        {
            Log.SetLogger(new Logger());
            UIKit.Instance.SetAssetLoader(AssetLoader.Instance);
            SceneManager.Instance.OnSceneLoaded += OnSceneLoaded;
            PoolManager.Instance.Initialize();
            var canvasTransform = GameObject.FindWithTag("UICanvas").GetComponent<Canvas>();
            GlobalContext.Register("UICanvas", canvasTransform);
            // 全局的静态Context
            GlobalContext.AddSystem(new SocketSystem());
        }

        private void Update()
        {
            GlobalContext.OnUpdate(Time.deltaTime);
        }

        private static void OnSceneLoaded(string sceneName)
        {
            Log.Info("Scene loaded: " + sceneName);
        }

        private void OnDestroy()
        {
            GlobalContext.Dispose();
        }
    }
}